﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;

namespace XQ.RequestOfficeSupplies.WebAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务
            //跨域配置
            var allowedMethods = ConfigurationManager.AppSettings["cors:allowedMethods"];
            var allowedOrigin = ConfigurationManager.AppSettings["cors:allowedOrigin"];
            var allowedHeaders = ConfigurationManager.AppSettings["cors:allowedHeaders"];
            //config.EnableCors();
            //// 启用CORS支持  
            var corsAttr = new EnableCorsAttribute(allowedOrigin, allowedHeaders, allowedMethods); // 允许所有源、头部、方法，出于安全考虑，应限制这些值  
            corsAttr.SupportsCredentials = true;
            config.EnableCors(corsAttr);

            //显示详细错误
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
